#Region " Copyright "
' Copyright (c) 2005, Oliver Hine (oliver.hine@gmail.com)
' www.oliverhine.com
'
' This program is free software; you can redistribute it and/or
' modify it under the terms of the GNU General Public License
' as published by the Free Software Foundation; either version 2
' of the License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
#End Region

Namespace OliverHine.Weather.Common
    Public Class WebRequest
        Private objWebRequest As Net.HttpWebRequest
        Private _ErrorMessage As String

        Public Function Create(ByVal Url As String, ByVal ProxyServer As String, ByVal ProxyPort As Integer, ByVal ProxyUsername As String, ByVal ProxyPassword As String) As Net.HttpWebRequest
            Try
                ' First we request our content from our provider source .. in this case .. The Weather Channel
                objWebRequest = CType(System.Net.WebRequest.Create(Url.ToString), System.Net.HttpWebRequest)

                ' set proxy server
                If Common.Null.IsNull(ProxyServer) = False Then

                    If Common.Null.IsNull(ProxyPort) = False Then
                        objWebRequest.Proxy = New System.Net.WebProxy(ProxyServer.ToString, ProxyPort)
                    Else
                        objWebRequest.Proxy = New System.Net.WebProxy(ProxyServer)
                    End If

                    If Common.Null.IsNull(ProxyUsername) = False AndAlso Common.Null.IsNull(ProxyPassword) = False Then
                        Dim userName As String = ProxyUsername
                        Dim password As String = ProxyPassword

                        If userName.IndexOf("\"c) = -1 Then
                            objWebRequest.Credentials = New System.Net.NetworkCredential(userName, password)
                        Else
                            Dim domain As String = Microsoft.VisualBasic.Left(userName, Microsoft.VisualBasic.InStr(userName, "\") - 1)
                            objWebRequest.Credentials = New System.Net.NetworkCredential(userName, password, domain)
                        End If
                    End If
                End If

                ' set the HTTP properties
                ' wr.Timeout = Common.Globals.HostSettings("ProxyTimeout")

                objWebRequest.UserAgent = "oliverhine.com explorer"

                Create = objWebRequest

            Catch ex As Net.WebException
                _ErrorMessage = ex.ToString
            End Try
        End Function

        Public Function Create(ByVal Url As String) As Net.HttpWebRequest
            Try
                ' First we request our content from our provider source .. in this case .. The Weather Channel
                objWebRequest = CType(System.Net.WebRequest.Create(Url.ToString), System.Net.HttpWebRequest)

                ' set proxy server
               
                ' set the HTTP properties
                ' wr.Timeout = Common.Globals.HostSettings("ProxyTimeout")

                objWebRequest.UserAgent = "oliverhine.com explorer"

                Create = objWebRequest

            Catch ex As Net.WebException
                _ErrorMessage = ex.ToString
            End Try
        End Function

        Public ReadOnly Property ErrorMessage() As String
            Get
                Return _ErrorMessage
            End Get
        End Property
    End Class
End Namespace